package com.wyj.aspect;

import lombok.extern.log4j.Log4j2;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author 王永杰
 * @Description 描述该类的做什么用
 * @since 2022/7/25
 */
@Component
@Log4j2
@Aspect
public class serviceLogAspect {

    //service包下所有的类的方法 定义切点
    @Pointcut("execution(* com.wyj.service.imp.*.*(..))")
    public void pointcut() {
    }


    @Before("pointcut()")
    public void before(JoinPoint joinPoint) {
        //获取请求的request
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        if (requestAttributes == null) {
            return ;
        }

        HttpServletRequest request = requestAttributes.getRequest();
        String ip = request.getRequestURI();
        String now = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
        String target = joinPoint.getSignature().getDeclaringTypeName()+joinPoint.getSignature().getName();

        log.info("用户ip"+ip+"访问的时间"+"访问了"+target);
    }
}
